﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectNocturnalFool
{
    class LabelControl : UserControl
    {

        /// <summary>
        /// Determines whether to draw border around control
        /// </summary>
        public bool drawBorder = false;
        public Color borderColor = Color.Black;

        public SpriteFont font;

        public Texture2D bgTexture
        {
            get { return _bgTexture; }
            set
            {
                _bgTexture = value;
                Vector2 size = font.MeasureString(message);
                width = (int)(Math.Max(Math.Max(value.Width, size.X), width));
                height = (int)(Math.Max(Math.Max(value.Height, size.Y), height));
            }
        }


        protected string _message = "";
        public string message
        {
            get { return _message; }
            set
            {
                _message = value;
                Vector2 size = font.MeasureString(value);
                width = (int)Math.Max(Math.Max(bgTexture.Width, size.X), width);
                height = (int)Math.Max(Math.Max(bgTexture.Height, size.Y), height);

            }
        }
        

        public override void LoadContent()
        {
            base.LoadContent();
            _bgTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TextureBlank);
            
            font = TacticsGame.Instance.gameFont;
            depth = ControlDepth.Label;

        }

        public override void Draw(GameTime gameTime)
        {
            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);
            Rectangle targetRect = new Rectangle(x, y, width, height);
            Vector2 Size = font.MeasureString(message);
            
            int textX, textY;

                TacticsGame.Instance.spriteBatch.Draw(bgTexture,
                                                      targetRect,
                                                      null,
                                                      Color.White,
                                                      0,
                                                      Vector2.Zero,
                                                      SpriteEffects.None,
                                                      0);

             textX = Util.getCentreDrawPointX(x, width, (int)Size.X);
             textY = Util.getCentreDrawPointY(y, height, (int)Size.Y);

                TacticsGame.Instance.spriteBatch.DrawString(font,
                                                message,
                                                new Vector2(textX, textY),
                                                Color.Black,
                                                0.0f,
                                                new Vector2(0, 0),
                                                Constants.FontScale,
                                                SpriteEffects.None,
                                                0);
            if(drawBorder)
                Util.drawBorder(targetRect, borderColor, 1);

            TacticsGame.Instance.spriteBatch.End();
        }

    }
}
